<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/3/23
 * Time: 11:02
 */
namespace app\admin\controller;
use think\Db;
class UserController extends CommonController
{
    /**
     *getUserData 页面加载获取用户表数据
     * @return json格式数组
     */
    public function getUserData(){
        $where =array();
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if(isset($_POST['username']) && $_POST['username']!=''){
            $username = $_POST['username'];
            $where['user_username'] = array('like',"%$username%");
        }
        if(isset($_POST['phone']) && $_POST['phone']!=''){
            $phone = $_POST['phone'];
            $where['user_phone'] = array('like',"%$phone%");
        }
        if(isset($_POST['sex']) && $_POST['sex']!=''){
            $where['user_gender'] = $_POST['sex'];
        }
        $res = Db::table('ew_u_user')
            ->where($where)
            ->field('user_id,user_username,user_image,user_phone,user_gender,user_time,user_status')
            ->order('user_time DESC')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total =Db::table('ew_u_user')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * enableUser 启用用户
     * @return 返回状态
     */
    public function enableUser(){
        $data = $this->oneuser($_POST['cid'],1,"已启用！","启用失败！");
        return json_encode($data);
    }

    /**
     * disabledTeacher 停用教师
     * @return 返回状态
     */
    public function disabledUser(){
        $data = $this->oneuser($_POST['cid'],2,"已停用！","停用失败！");
        return json_encode($data);
    }

    /**
     * alluser 停用或启用用户
     * @param、$uid用户id、$num状态值、$succemsg成功返回信息、$failedmsg失败返回信息
     * @return 返回状态
     */
    protected function oneuser($uid,$num,$succemsg,$failedmsg){
        if($uid!=''){
            $where['user_id'] = $uid;
            $data['user_status'] = $num;//停用|启用
            $res =  Db::table('ew_u_user')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = $succemsg;
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = $failedmsg;
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
        }
        return $returnData;
    }
    /**
     * deleteUser 删除用户
     * @return 返回状态
     */
    public function deleteUser(){
        $res = Db::table('ew_u_user')->delete($_POST['id']);
        if($res){
            return json_encode(['status'=>1,'msg'=>"删除成功！"]);
        }else{
            return json_encode(['status'=>0,'msg'=>"删除失败！"]);
        }
    }

    /**
     * seeUserInfo 用户详情
     * @return 返回状态
     */
    public function seeUserInfo(){
        $id = input('id');
        $data = Db::table('ew_u_user')
            ->where('user_id',$id)
            ->field('user_username,user_image,user_phone,user_gender,user_time,user_content,user_status,ew_life_photos,user_zan')
            ->find();
        if($data['ew_life_photos']){
            $data['ew_life_photos'] = explode(',',$data['ew_life_photos']);
        }
        $this->assign ('data',$data);
        return $this->fetch("user/userinfo");
    }

    /**
     * 查看某用户发布的职位
     * @return string
     */
    public function myJobs() {
        $uid = $_POST['uid'];
        $jobs = Db::table("ew_job")
            ->where('job_user_id',$uid)
            ->field('job_id,job_user_id,job_name,job_type,job_status,job_createtime')
            ->order('job_createtime DESC')
            ->select();
        $this->assign ('data',$jobs);
        return $this->fetch("user/my_jobs");
    }


    /**
     * 查看某用户的评论——html
     * @return string
     */
    public function myComments() {
        $uid = $_POST['uid'];
        $this->assign ('uid',$uid);
        return $this->fetch("user/my_comments");
    }
    /**
     * 查看某用户的评论——数据
     * @return string
     */
    public function myCommentsTable() {
        $uid = $_POST['uid'];
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table("ew_comment")->alias('c')
            ->join('ew_u_user uu','c.comment_reply_user_id=uu.user_id','LEFT')
            ->where('comment_user_id',$uid)
            ->field('c.*,user_id,user_username')
            ->order('comment_time DESC')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table("ew_comment")->alias('c')
            ->join('ew_u_user uu','c.comment_reply_user_id=uu.user_id','LEFT')
            ->where('comment_user_id',$uid)
            ->field('c.*,user_id,user_username')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    /**
     * 查看某用户的评论的回复
     * @return string
     */
    public function twoComments() {
        $comment_id = $_POST['cid'];
        $comtts = Db::table('ew_comment_tt')->alias('ct')
            ->join('ew_u_user u','ct.comtt_one_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','ct.comtt_reply_user_id=u2.user_id','LEFT')
            ->field('comtt_id,u.user_id one_id,u2.user_id reply_id,u.user_username one_name,u2.user_username reply_name,comtt_content,comtt_time')
            ->where('comtt_comment_id',$comment_id)
//            ->limit(($page-1)*5,5)
            ->select();
        $this->assign ('data',$comtts);
        return $this->fetch("user/twoComments");
    }
    /**
     * 删除评论
     * cid
     * @return string
     */
    public function deleteCom(){
        $cid = $_POST['cid'];
        // 启动事务
        Db::startTrans();
        try{

            $data = Db::table('ew_comment')
                ->where('comment_id',$cid)
                ->field('comment_type,comment_user_id')
                ->find();
            //拿到数据之后删掉这条评论
            Db::table('ew_comment')->where('comment_id',$cid)->delete();
            $type = $data['comment_type'];
            $uid = $data['comment_user_id'];
            //删除掉这个评论的所有回复
            Db::table('ew_comment_tt')->where('comtt_comment_id',$cid)->delete();
            //重新计算这个用户的评分
            $coms = Db::table('ew_comment')
                ->where('comment_user_id',$uid)
                ->where('comment_type',$type)
                ->field('sum(comment_score) star,count(*) many')
                ->find();
            $many = $coms['many'];
            if($many <= 0){
                $star = 5;
            }else{
                $star = sprintf("%.1f", ($coms['star']+5)/($many+1));
            }
            $update = array();
            if($type == 1){
                $update['user_one_star'] = $star;
                $update['user_one_num'] = $many;
            }
            if($type == 2){
                $update['user_two_star'] = $star;
                $update['user_two_num'] = $many;
            }
            if($type == 3){
                $update['user_thr_star'] = $star;
                $update['user_thr_num'] = $many;
            }
            Db::table('ew_u_user')->where('user_id',$uid)->update($update);
            //========================
            // 提交事务
            Db::commit();
            return json_encode(['status'=>200,'msg'=>'成功']);
            //=====end===============
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
     /**
     * 删除某用户的评论的回复
     * @return string
     */
    public function delcomtt() {
        $comment_id = $_POST['ttid'];
        $res = Db::table('ew_comment_tt')
            ->where('comtt_id',$comment_id)
            ->delete();
        if($res){
            return json_encode(['status'=>1,'msg'=>"删除成功！"]);
        }else{
            return json_encode(['status'=>0,'msg'=>"删除失败！"]);
        }
    }

}